import axios from 'axios';
import {ElMessage} from 'element-plus'
// import sysConfig from "@/config";
// import tool from '@/utils/tool';
// import router from '@/router';
// import md5 from 'js-md5'

// // axios.defaults.baseURL = 'http://oao.zhichengshangxin.com/'

// // axios.defaults.timeout = sysConfig.TIMEOUT
// function formatter(thistime, fmt) {
// 	let $this = new Date(thistime)
// 	let o = {
// 		'M+': $this.getMonth() + 1,
// 		'd+': $this.getDate(),
// 		'h+': $this.getHours(),
// 		'm+': $this.getMinutes(),
// 		's+': $this.getSeconds(),
// 		'q+': Math.floor(($this.getMonth() + 3) / 3),
// 		'S': $this.getMilliseconds()
// 	}
// 	if (/(y+)/.test(fmt)) {
// 		fmt = fmt.replace(RegExp.$1, ($this.getFullYear() + '').substr(4 - RegExp.$1.length))
// 	}
// 	for (var k in o) {
// 		if (new RegExp('(' + k + ')').test(fmt)) {
// 			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
// 		}
// 	}
// 	return fmt
// }
// var url = "api/note/noteList"; //接口地址
// var date = formatter(new Date(), 'yyyy-MM-dd hh:mm:ss') //获取当前年月日 format调用方法在下面
// var api_token = md5(url + date + 'LocalHostKey'); //md5加密



// HTTP request 拦截器
const service = axios.create({
    baseURL: "http://47.94.22.162:3085/", //自行修改
    timeout: 99999
});

service.interceptors.request.use(
    (config) => {
        return config;
    },
    (error) => {
        // 处理请求错误
        console.log(error); // for debug
        return Promise.reject(error);
    }
);


service.interceptors.response.use(
    (response) => {
        const res = response.data;
        // 未设置状态码则默认成功状态
        // const code = res.errCode || 0;
        // if the custom code is not 20000, it is judged as an error.
        // console.log(res,'ressssssssssssss');
        // if (res.status === 200) {
        //     ElMessage({
        //         message: res.msg,
        //         type: "success",
        //         duration: 5 * 1000,
        //     });
            
        // }else{
        //     ElMessage({
        //         message: res.msg,
        //         type: "error",
        //         duration: 5 * 1000,
        //     });
        // }
        if (res.status) {
            if (res.status === 200) {
                ElMessage({
                    message: res.msg,
                    type: "success",
                    duration: 2 * 1000,
                });
            }else{
                ElMessage({
                    message: res.msg,
                    type: "error",
                    duration: 2 * 1000,
                });
            }
            return res;
        } else {
            return res;
        }
    },
    (error) => {
        console.log(error, 8465)
        if (error && error.response) {
            switch (error.response.status) {
                case 302:
                    error.message = "接口重定向了！";
                    break;
                case 400:
                    error.message = "请求错误";
                    break;
                case 401:
                    error.message = "未授权，请登录";
                    break;
                case 403:
                    error.message = "拒绝访问";
                    break;
                case 404:
                    error.message = `请求地址出错: ${error.response.config.url}`;
                    break;
                case 408:
                    error.message = "请求超时";
                    break;
                case 409:
                    error.message = "系统已存在相同数据！";
                    break;
                case 500:
                    error.message = "服务器内部错误";
                    break;
                case 501:
                    error.message = "服务未实现";
                    break;
                case 502:
                    error.message = "网关错误";
                    break;
                case 503:
                    error.message = "服务不可用";
                    break;
                case 504:
                    error.message = "网关超时";
                    break;
                case 505:
                    error.message = "HTTP版本不受支持";
                    break;
                default:
                    console.log(error, 111)
                    error.message = "异常问题，请联系管理员！";
                    break;
            }
        }
        ElMessage({
            message: error || "Error",
            type: "error",
            duration: 5 * 1000,
        });
        return Promise.reject(error);
    }
);

export default service;

// export default axios;
